<?php

namespace app\common\command;

use think\Exception;

class Productprice
{

    /**
     * 商品调价
     */
    public static function cron(){
        try {
            $model = new \app\admin\model\Productpriceitem();
            $priceMdl = new \app\admin\model\Productprice();
            $productMdl = new \app\admin\model\Product();

            $priceIds = $priceMdl->where(['effective_date' => date('Y-m-d')])->column('product_price_id');
            $where = [
                'product_price_id' => ['in', $priceIds]
            ];
            $field = '*';
            // 所有调价商品
            $list = $model->where($where)->field($field)->select();
            if($list){
                foreach ($list as $item) {
                    $data = [
                        'price' => $item['new_price'],
                        'wholesale_price' => $item['new_wholesale_price'],
                        'member_integral' => $item['new_member_integral'],
                    ];
                    $res = $productMdl->where('id', $item['product_id'])->update($data);

                    if ($res !== false) {
                        trace($item['product_id'] . ': 商品已调价成功','log');
                    } else {
                        trace($item['product_id'] . ': 商品调价失败','notice');
                    }
                }
            }
        } catch (Exception $e) {
            trace($e->getMessage(),'error');
        }
    }
}